from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from .models import Student, Student_last
from django.urls import reverse
from .forms import EditorForm

from django.contrib.auth.decorators import login_required
import mistune

# Create your views here.

def index(request):
	all_student = Student.objects.all().order_by('seat_number')
	student_true = []
	student_false = []
	for student in all_student:
		if student.bool_intro:
			student_true.append(student)
		else:
			student_false.append(student)

	context = {"student_true":student_true, "student_false":student_false}
	return render(request, 'student/index.html', context=context)

@login_required
def look(request, seat_number):
	# look_student = Student.objects.get(seat_number=seat_number)
	look_student = get_object_or_404(Student, seat_number=seat_number)
	context = {"look_student" : look_student}
	render_md = mistune.create_markdown(plugins=['strikethrough', 'table'])
	markdown_html = render_md(look_student.student_intro)
	look_student.html_text = markdown_html
	look_student.save()
	context['markdown'] = markdown_html
	return render(request, 'student/student.html', context=context)

@login_required
def editor(request, seat_number):
	if request.method == 'POST':
		look_student = get_object_or_404(Student, seat_number=seat_number)
		last_student = Student_last()
		if request.POST["intro"] == None or request.POST['qq'] == None or request.POST['phone'] == None:
			return render(request, "student/error_null.html")

		if look_student.bool_intro == True:
			last_student.student_info = look_student.student_intro
			last_student.qq_number = look_student.qq_number
			last_student.phone_number = look_student.phone_number
			last_student.last_edit = look_student.last_edit
			last_student.last_edit_name = look_student.last_edit_name
			last_student.history = look_student
			last_student.save()

		look_student.student_intro = request.POST["intro"]
		look_student.qq_number = request.POST["qq"]
		look_student.phone_number = request.POST['phone']
		look_student.last_edit_name = request.user.username
		look_student.bool_intro = True
		look_student.save()
	
		return HttpResponseRedirect(reverse("student:look", kwargs={"seat_number":seat_number}))
	else:
		look_student = get_object_or_404(Student, seat_number=seat_number)
		editor_form = EditorForm()
		editor_form.fields['intro'].initial = look_student.student_intro
		editor_form.fields['qq'].initial = look_student.qq_number
		editor_form.fields['phone'].initial = look_student.phone_number
		context = {"look_student":look_student, 'editor_form':editor_form}
		return render(request, "student/editor.html", context=context)

def history(request, seat_number):
	look_student = get_object_or_404(Student, seat_number=seat_number)
	all_history = look_student.history.all().order_by('-last_edit')
	context = {'look_student':look_student, 'history':all_history}
	return render(request, "student/history.html", context=context)

def history_spe(request, seat_number, serial_number):
	historys = get_object_or_404(Student_last, id=serial_number)
	render_md = mistune.create_markdown(plugins=['strikethrough', 'table'])
	markdown_html = render_md(historys.student_info)
	historys.html_text = markdown_html
	historys.save()
	context = {'history': historys, 'seat_number': seat_number, 'name': get_object_or_404(Student, seat_number=seat_number).name}
	return render(request, 'student/history_spe.html', context=context)